<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta http-equiv=”X-FRAME-OPTIONS” content=”SAMEORIGIN”>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../css/upload.css"/>
</head>
<body>
<div class="admin-main">
    <form id="importForm" name="importForm" method="post" target="file_upload" enctype="multipart/form-data">
        <div><em>导入文件不能超过5M，仅允许导入“xls”或“xlsx”格式文件！</em></div>
        <input type="file" size="30" id="uploadFiles" name="uploadFiles" style="padding: 15px"/>
        <div>
            <button class="layui-btn" id="import">确认导入</button>
        </div>
    </form>
    <iframe name="file_upload" style="display:none"></iframe>
</div>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../js/jquery/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="../../js/jquery/jquery-form.js"></script>
<script type="text/javascript">
    layui.use('layer', function () {
        $('#import').click(function () {
            var fileName = $("#uploadFiles").val();
            if (fileName == '') {
                layer.msg('请选择文件');
                return false;
            }
            if (!checkFileExt(fileName)) {
                layer.msg('文件格式错误');
                return false;
            }
//            var index = parent.layer.load(1, {
//                shade: [0.1, '#fff'] //0.1透明度的白色背景
//            });
//            $("#importForm").attr("action", "importExecl");
//            $(this).submit();
            var options = {
                url: "importExecl",   //同action
                type: 'post',
                beforeSend: function (xhr) {//请求之前
                    parent.layer.load(2, {
                        shade: [0.5, '#fff'] //0.5透明度的黑色背景
                    });
                },
                success: function (data) {
                    layer.msg(data.message);
                },
                complete: function (xhr) {//请求完成
                    parent.layer.closeAll('loading');
                    $("#uploadFiles").val('');
                },
                error: function (xhr, status, msg) {
                }
            };
            $("#importForm").ajaxSubmit(options);
        });
        function checkFileExt(filename) {
            var flag = false; //状态
            var arr = ["xls", "xlsx"];
            //取出上传文件的扩展名
            var index = filename.lastIndexOf(".");
            var ext = filename.substr(index + 1);
            //循环比较
            for (var i = 0; i < arr.length; i++) {
                if (ext == arr[i]) {
                    flag = true; //一旦找到合适的，立即退出循环
                    break;
                }
            }
            return flag;
        }
    })
    ;
</script>
</body>
</html>